const formatDate = require('../../utils/util')
Page({
  data: {
    arr: [],
    opp: [],
    checked: true,
    check: false,
  },
  onLoad() {
    var arr = wx.getStorageSync('arr')
    var opp = wx.getStorageSync('opp')
    if (arr) {
      arr = JSON.parse(arr)
      this.setData({
        arr
      })
    }
    if (opp) {
      opp = JSON.parse(opp)
      this.setData({
        opp
      })
    }
    var date = formatDate(new Date())
    this.setData({
      date,
    })
  },
  handleDelete(e) {
    var arr = this.data.arr
    var opp = this.data.opp
    var {name,list} = e.target.dataset
    if(list){
      arr.forEach((item, index) => {
        if (item == name) {
          arr.splice(index, 1)
        }
        this.setData({arr})
        wx.setStorageSync('arr', JSON.stringify(arr))
      })
    }else{
      opp.forEach((item, index) => {
        if (item == name) {
          opp.splice(index, 1)
        }
        this.setData({opp})
        wx.setStorageSync('opp', JSON.stringify(opp))
      })
    }
  },
  handleAdd(event) {
    var arr = this.data.arr
    var opp = this.data.opp
    if ((!arr.includes(event.detail.value)) && (!opp.includes(event.detail.value))) {
      arr.unshift(event.detail.value)
      wx.setStorageSync('arr', JSON.stringify(arr))
      this.setData({
        arr
      })
    } else {
      wx.showModal({
        title: "提示",
        content: "该任务已存在,请勿添加相同任务"
      })
    }
  },
  onChange(event) {
    var arr = this.data.arr
    var opp = this.data.opp
    var name = event.currentTarget.dataset.name
    arr.forEach((item, index) => {
      if (item == name) {
        opp.unshift(item)
        arr.splice(index, 1)
      }
    })
    wx.setStorageSync('arr', JSON.stringify(arr))
    wx.setStorageSync('opp', JSON.stringify(opp))
    this.setData({
      arr,
      opp
    });
  },
  onChangeOver(event) {
    var arr = this.data.arr
    var opp = this.data.opp
    var name = event.currentTarget.dataset.name
    opp.forEach((item, index) => {
      if (item == name) {
        arr.unshift(item)
        opp.splice(index, 1)
      }
    })
    wx.setStorageSync('arr', JSON.stringify(arr))
    wx.setStorageSync('opp', JSON.stringify(opp))
    this.setData({
      arr,
      opp
    });
  }
})